// pages/rate/rate.js
const app = getApp()
var network = require("../../utils/request.js"); //网络请求
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    isLoging: false,
    searchIcon: "../../resource/images/search.svg",
    noData: "../../resource/images/noData.png",
    toPurchaseImg: "../../resource/images/to_purchase.gif",//发布供应图片
    query: {
      pageSize: 10,
      pageNum: 1
    },
    icon_img: "../../resource/images/arrow_down.png", 
    
    inputValue: '',//点击结果项之后替换到文本框的值
    bindSource: [],//绑定到页面的数据，根据用户输入动态变化
    isShowHint: false,//是否显示联想内容

    arrayOne: [{ "standard": "全部" }],
    region: ['全部', '全部', '全部'],

    indexOne: 0,
    customItem: '全部',
    items: [],
    call_img: "../../resource/images/call.png",//打电话图标
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      'title':"我的求购"
    })
    
  },

  //当键盘输入时，触发input事件
  bindiName: function (e) {
    var that = this
    var prefix = e.detail.value//用户实时输入值
    if (prefix != "") {
      //通过接口查询数据
      network.post('getTradeByName/' + prefix).then(resp => {
        console.log(resp.rows)
        if (resp.rows.length > 0) {
          that.setData({
            bindSource: resp.rows,
            isShowHint: true
          })
        } else {
          that.setData({
            bindSource: [],
            isShowHint: false
          })
        }
      })
    }

  },

  itemtap: function (e) {
    this.setData({
      inputValue: e.target.id,
      bindSource: [],
      isShowHint: false
    })

    //获取规格下拉数据源
    network.post('getGoodsByName/' + e.target.id).then(resp => {
      console.log(resp)
      if (resp.rows.length > 0) {
        resp.rows.unshift({
          "standard": "全部"
        })
        this.setData({
          arrayOne: resp.rows
        })
      } else {
        let arr = { "standard": "全部" }
        this.setData({
          arrayOne: arr
        })
      }
    })
  },
  //下拉事件
  bindPickerChangeOne: function (e) {
    this.setData({
      indexOne: e.detail.value
    })
    this.data.query.pageNum = 1
    this.data.items = []
    var standard = this.data.arrayOne[e.detail.value].standard
    this.loading({
      "standard": standard
    })

  },

  //区域选择
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
    this.data.query.pageNum = 1
    this.data.items = []
    this.loading({
      'province': e.detail.value[0],
      'city': e.detail.value[1],
      'area': e.detail.value[2],
    })
  },

  lookInfo(e){
    //查看详情
    var id = this.data.items[e.currentTarget.dataset.index].id
    wx.navigateTo({
      url: "purchaseManageDetail/purchaseManageDetail?id=" + id
    });
  },

  //跳转到发布供应
  toPurchase() {
    wx.navigateTo({
      url: '../purchase/purchase',
    })
  },

  loading(query = {}) {
    Object.assign(this.data.query, query)
    network.post('queryMyDemand', this.data.query).then(resp => {
      console.log(resp)
      this.data.items.push(...resp.rows)
      this.setData({
        items: this.data.items,
        total: resp.total,
        isLoging: false,
      })
    })
  },
  onPullDownRefresh() {
    this.data.query.pageNum = 1;
    this.data.items = [];
    this.loading()
    wx.pageScrollTo({
      scrollTop: 0
    })
    wx.stopPullDownRefresh()
  },
  removeDemand(e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否撤销本次需求信息？',
      success: function (res) {
        if (res.confirm) {
          network.post('/updateStatus/demand/' + e.currentTarget.dataset.id + '/-2').then(resp => {
            wx.showToast({
              title: '撤销成功！',
              icon: 'success',
              duration: 2000
            })
            that.data.items[e.currentTarget.dataset.index].status = '-2'
            that.setData({
              items: that.data.items
            })
          })
        }
      }
    })
  },
  outDemand(e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否下架需求信息？',
      success: function (res) {
        if (res.confirm) {
          network.post('/updateStatus/demand/' + e.currentTarget.dataset.id + '/2').then(resp => {
            wx.showToast({
              title: '下架成功！',
              icon: 'success',
              duration: 2000
            })
            that.data.items[e.currentTarget.dataset.index].status = '2'
            that.setData({
              items: that.data.items
            })
          })
        }
      }
    })
  },
  putDemand(e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否上架需求信息？',
      success: function (res) {
        if (res.confirm) {
          network.post('/updateStatus/demand/' + e.currentTarget.dataset.id + '/1').then(resp => {
            wx.showToast({
              title: '上架成功！',
              icon: 'success',
              duration: 2000
            })
            that.data.items[e.currentTarget.dataset.index].status = '1'
            that.setData({
              items: that.data.items
            })
          })
        }
      }
    })
  },
  editDemand(e){
    //编辑
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否重新编辑需求信息？',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../purchase/purchase?id=' + e.currentTarget.dataset.id,
          })
        }
      }
    })
  },
  editUpdate(e){
    //更新
    network.post('/refreshPurchases/2/' + e.currentTarget.dataset.id).then(resp => {
      wx.showToast({
        title: '刷新成功！',
        icon: 'success',
        duration: 2000
      })
    })
  },
  onReachBottom() {
    //加载更多
    var total = parseInt(this.data.total)
    var pageNum = this.data.query.pageNum
    var pageCount = Math.ceil(total / this.data.query.pageSize)
    console.log(total, pageNum)
    if (pageNum < pageCount && !this.data.isLoging) {
      this.data.isLoging = true
      this.data.query.pageNum = ++pageNum
      this.loading()
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.query.pageNum = 1;
    this.data.items = [];
    this.loading()
    wx.pageScrollTo({
      scrollTop: 0
    })
    wx.stopPullDownRefresh()
  },

  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '跑合中药材供需云平台',
      imageUrl: '../../resource/images/share.jpg',
      path: 'pages/supplyList/supplyList',
      success: function (res) {
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享取消',
        })
      }
    }
  }
})